<script lang="ts" setup>
import type { Dict } from '@croco/framework/types';

import { ref } from 'vue';

import { service } from '#/api/request';
import { DataTable } from '#/components/data-table';

import { useTable } from './data';

defineOptions({ name: 'SystemLoginLog' });

const props = defineProps<{
    enums: Record<string, Array<Dict> | undefined>;
    shortcuts: Array<Record<string, any>>;
}>();

const dataTableRef = ref<InstanceType<typeof DataTable>>();
const columns = useTable(props.enums);

// 清空
function onClear() {
    service.confirm({
        message: `确定清空所有记录（非admin用户）?`,
        url: '/system/log/login/clear',
        success: dataTableRef.value?.refresh,
    });
}

// 删除
function onDelete(rows: { id: number }[]) {
    const logs = rows.map((item) => item.id);
    service.confirm({
        message: `确定对所选${logs.length}条记录进行${logs.length === 1 ? '删除' : '批量删除'}操作?`,
        url: '/system/log/login',
        data: { logs },
        success: dataTableRef.value?.refresh,
    });
}
</script>
<template>
    <DataTable ref="dataTableRef" :columns="columns" :form-item-width="320" class="login-table" api="/system/log/login" selection select-on-row-click>
        <template #extra="{ selections }">
            <el-button v-auth="'system:login:log:remove'" @click="onDelete(selections)" :disabled="selections.length <= 0" type="danger" />
            <el-button v-auth="'system:login:log:clear'" @click="onClear()" type="warning" />
        </template>
        <template #status="{ form }">
            <el-select v-model="form.status" :empty-values="[null, undefined]">
                <el-option label="全部" value="" />
                <el-option v-for="(item, index) in enums.status" :key="index" :label="item.label" :value="item.value" />
            </el-select>
        </template>
        <template #timeFrame="{ form }">
            <el-date-picker v-model="form.timeFrame" type="datetimerange" :shortcuts="shortcuts" value-format="YYYY-MM-DD HH:mm:ss" />
        </template>
    </DataTable>
</template>
